package developer.zyk.starter.controllers.system;

import developer.zyk.ability.config.AIProperties;
import developer.zyk.global.common.Result;
import developer.zyk.global.common.StringStringPair;
import developer.zyk.global.common.consts.FileType;
import developer.zyk.starter.controllers.system.vo.GlobalOptions;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhangyongkang
 * @time 2025/5/16 11:11
 * @description
 */
@RequestMapping("options")
@RestController
public class SystemOptionsController {
    @Resource
    private AIProperties aiProperties;

    @GetMapping("global")
    public Result<GlobalOptions> options() {
        GlobalOptions globalOptions = new GlobalOptions();
        //支持的模型
        globalOptions.setSupportModels(supportModel());
        globalOptions.setSupportFileTypes(supportFileType());
        return Result.success(globalOptions);

    }

    private String supportFileType() {
        return Arrays.stream(FileType.values())
                .map(FileType::getType)
                .collect(Collectors.joining(","));
    }

    private GlobalOptions.SupportModel supportModel() {
        GlobalOptions.SupportModel supportModel = new GlobalOptions.SupportModel();
        AIProperties.SupportModel config = aiProperties.getSupportModel();
        supportModel.setOptions(
                config.getModels()
                        .stream()
                        .filter(a -> Objects.equals(Boolean.TRUE, a.getEnable()))
                        .map(a -> StringStringPair.builder()
                                .key(a.getKey())
                                .value(a.getLabel())
                                .build())
                        .toList()
        );
        supportModel.setDefaultKey(config.getDefaultKey());
        return supportModel;
    }
}
